
$(function(){
// 隐藏导航跟底部
	var windowheight = $(window).height();
	var margintop = (windowheight-$(".loginPublic").height())/2-55;
	$(".site-footer").css("display","none");
	$(".header").css("display","none");
	$(".loginPublic").css("margin-top",margintop);
});
/*发送验证码*/
$(document).on( "click", "#phonecode", function(e) {
		var usernameval = $(this).parent().parent().find("#rusername").val();
		var username = $(this).parent().parent().find("#rusername");
		var phonecode_href = $(".phonecode").attr("href");

		if(checkusername(username)){
			loading = layer.msg('加载中...', {icon: 16});
			$.post(phonecode_href, {"phone":usernameval, "_token":$("[name='_token']").val()}, function(data){
			}, 'json').error(function(data) {
				layer.close(loading);
			    var obj = $.parseJSON(data.responseText);
				$.each(obj.error_messages, function(i, item){
					$(".default-tip").hide();
					$(".error-tip").show();
					$(".error-tip").text(item[0]);
				});   
			}).success(function(data) {
				layer.close(loading);
				var time=60; 
				var id = setInterval(function() {
			    	if (time >0) {
			    		time--; 
			    		$('.phonecode').text("重新发送("+time+")");
			    		$(".phonecode").css("background-color","#b5b5b5");
			    		$(".phonecode").attr("id","");
			    	} else {
		  			    $(".phonecode").text("获取验证码");
		  			    $(".phonecode").css("background-color","#48b4ff");
		  			    $(".phonecode").attr("id","phonecode");
		  			 clearInterval(id)
		  			};
				},1000);
			});
			return true;
		}else{
			return false;
		}
	})

/*重置密码提交*/
$(document).on( "click", "#resetpwd-start", function(e) {
  var loading = layer.msg('加载中...', {icon: 16});
  var username = $(this).parent().parent().find("#rusername");
  var code = $(this).parent().parent().find("#rphonecode").val();
  var _token = $("[name='_token']").val();
  var reset_href = $("#resetpwd-form");
  if (checkusername(username,1)) {
    if (code=="") {
      $(".error-tip").show();
      $(".error-tip").text("请输入验证码");
        return false;
    }
    $.post(reset_href[0].action, {"username":username.val(),"code":code,"_token":_token}, function(data){
    }, 'json').error(function(data) {
        var obj = $.parseJSON(data.responseText);
      $.each(obj.error_messages, function(i, item){
        $(".error-tip").show();
        $(".error-tip").text(item[0]);
      });
    }).success(function(data) {
      var obj = data.data;
      if (obj.status_code == "302"){
        $("#resetpwd-forms").css("display","block");
        localStorage.setItem("username", JSON.stringify(username.val()));
        // window.location.href= jumpUrl+"/login";
        window.location.href=obj.redirect_url;
      }
      if (data.success_messages) {
        alert(data.success_messages);
      }
    });
  }

});
$(function(){

	//修改密码
	$("#resetpwd-start-new").click(function(e){
		var params = getParams("#resetpwd-form");
		var password = $("#password_1").val();
		if(!getVal(params)){
			return false;
		}else{
			var loading = layer.msg('加载中...', {icon: 16});
			$.post(reset_pass_url, params, function(data){
			}, 'json').error(function(data) {
				layer.close(loading);
				var obj = $.parseJSON(data.responseText);
	                $.each(obj.error_messages, function(i, item){
	                  layer.msg(item[0]);
	                });
		      }).success(function(data) {
		      		layer.close(loading);
		      		layer.msg("修改成功");
		      		// var username =  JSON.parse(localStorage.getItem('username'));
		      		window.location.href= jumpUrl+"/login";
		   //    		$.post(jumpUrl+"/login", {"username":username,"password":password},function(data){
					// 	var token = data.token;
					// 	localStorage.setItem("token", JSON.stringify(token));
					// 	var locationhref = localStorage.getItem("locationhref");
					// 	window.location.href=locationhref;
					// }).error(function(data) {
					// 	   var obj = $.parseJSON(data.responseText);
					// 	   layer.close(loading);
					// 	   $(".error-tip").show();
					// 	   $(".error-tip").text(obj.error_messages.password);
					// });
		      		
		      });
		    };
	});

	function getVal(params){
		if(params.password_1.length < 1){
			$(".error-tip").show();
        	$(".error-tip").text("请输入新密码！");
			return false;
		}else if(params.password_1.length < 6 || params.password_1.length > 18){
			$(".error-tip").show();
        	$(".error-tip").text("新密码不能小于6位且不能大于18位！");
			return false;
		}
		if(params.password_2.length < 1){
			$(".error-tip").show();
        	$(".error-tip").text("请再次输入新密码！");
			return false;
		}else if(params.password_2.length < 6 || params.password_2.length > 18){
			$(".error-tip").show();
        	$(".error-tip").text("新密码不能小于6位且不能大于18位！");
			return false;
		}
		if(params.password_1.length != params.password_2.length){
			$(".error-tip").show();
        	$(".error-tip").text("两次输入的密码长度不相等！");
			return false;
		}
		return true;
	}
});